﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace TestGame
{
    class SpriteManager
    {
        public static ContentManager manager { get; set; }
        private static SortedSet<Sprite> _sprites = new SortedSet<Sprite>();
        private static SortedSet<Sprite> _clickables = new SortedSet<Sprite>();

        public static SortedSet<Sprite> Sprites { get { return _sprites; } }
        public static SortedSet<Sprite> Clickables { get { return _clickables; } }

        // add a sprite to the drawing routine
        public static void AddSprite(Sprite addMe)
        {
            Clickables.Add(addMe);
            if (!addMe.GetType().Equals(typeof(HUD)))
            {
                Sprites.Add(addMe);
            }
        }

        // draw all sprites in order
        public void Draw(SpriteBatch batch, GraphicsDevice graphics)
        {
            foreach (Sprite s in Sprites)
            {
                s.Draw(batch);
            }

            // click a sprite every tick, this allows for either a continuous or one-time effect
            // depending on the sprite's clicked function
            if (Game.player.lastClicked != null)
            {
                Sprite clicked = Game.player.lastClicked; 
                clicked.Clicked(batch, graphics, clicked);
            }
        }

        public void Update(GameTime gametime, Camera c, Map m) 
        {
            foreach(Sprite s in Sprites)
            {
                s.currentMap = m;

                // camera follows the player
                if (s.GetType().Equals(typeof(Player)))
                {
                    c.LookAt(s.Position);
                }
                s.Update(gametime);
            }
        }
    }
}
